package org.electronic.registration.dao;

import org.apache.commons.collections.CollectionUtils;
import org.electronic.registration.bo.Doctor;
import org.electronic.registration.bo.Record;
import org.electronic.registration.bo.Scheduler;
import org.electronic.registration.util.TransformerImpl;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * User: Egor
 * Date: 08.12.11
 */
@Service
public class SchedulerDaoImpl extends HibernateDaoSupport implements SchedulerDao {

    @Autowired
    public void init(SessionFactory factory) {
        setSessionFactory(factory);
    }

    public void save(Scheduler scheduler) {
        Record record = TransformerImpl.schedulerToRecord(scheduler);
        getHibernateTemplate().save(record);
    }

    public List<Scheduler> getDoctorScheduler(Doctor doctor) {
        List<Scheduler> result = new ArrayList<Scheduler>();
        List<Record> records = getHibernateTemplate().findByNamedQuery(Record.GET_SCHEDULER_FOR_DOCTOR, doctor);
        CollectionUtils.collect(records, TransformerImpl.recordToSchedulerTransformer(), result);
        return result;
    }

    public List<Scheduler> getDoctorSchedulerFromDate(Doctor doctor, Date date) {
        List<Scheduler> result = new ArrayList<Scheduler>();
        List<Record> records = getHibernateTemplate().findByNamedQuery(Record.GET_SCHEDULER_FOR_DOCTOR_FROM_DATE, doctor, date);
        CollectionUtils.collect(records, TransformerImpl.recordToSchedulerTransformer(), result);
        return result;
    }
}
